﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.Map;
using System.Collections.ObjectModel;
using CCF20Model;
using WiseLinkWPF.Windows.Controls;
using CCF20Model.ConfigurationFile;

namespace CCF20Module
{
    public partial class HelpMap : Window
    {
        private MapItemsCollection itemCollection = new MapItemsCollection();
        private BingSearchProvider searchProvider;
        private siteModel _siteModel;
        
        public HelpMap(siteModel location)
        {
            InitializeComponent();
            this.SetProvider();
            _siteModel = location;
            this.informationLayer.DataMappings.Add(new DataMapping("Location", DataMember.Location));

            Binding binding = new Binding();
            binding.Source = this.itemCollection;
            this.informationLayer.SetBinding(ItemsControl.ItemsSourceProperty, binding);
        
		
        }
        private void SetProvider()
        {
            if ((bool)ConfiguraitonHelper.MyConfiguration.isenglish)
            {

                BingMapProvider provider = new BingMapProvider(MapMode.Road, true, BingMapHelper.VEKey);
                provider.IsTileCachingEnabled = true;
                this.RadMap1.Provider = provider;
                // Init searh provider.
                
            }
            else
            {
                OpenStreetCustomProvider provider = new OpenStreetCustomProvider(MapMode.Road, true);
                provider.IsTileCachingEnabled = true;
                this.RadMap1.Provider = provider;

            }

            searchProvider = new BingSearchProvider();
            searchProvider.ApplicationId = BingMapHelper.VEKey;
            searchProvider.MapControl = this.RadMap1;
            searchProvider.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(Provider_SearchCompleted);
        }
        private void SearchHandler(object sender, RoutedEventArgs e)
		{
            string query = null;//this.SearchCondition.Text;

			if (!string.IsNullOrEmpty(query))
			{
				SearchRequest request = new SearchRequest();
                //request.Culture = new System.Globalization.CultureInfo("zh-CN");
                request.Query = query;

				this.searchProvider.SearchAsync(request);
			}
		}
        private void Provider_SearchCompleted(object sender, SearchCompletedEventArgs args)
        {
            SearchResponse response = args.Response;
            
            if (response != null && response.ResultSets.Count > 0)
            {
                this.itemCollection.Clear();
                if (response.ResultSets[0].Results.Count > 0)
                {
                    
                    foreach (SearchResultBase result in response.ResultSets[0].Results)
                    {
                        MyMapItem item = new MyMapItem()
                        {
                            Title = result.Name,
                            Location = result.LocationData.Locations[0]
                        };
                       

                        this.itemCollection.Add(item);
                    }
                }

                if (response.ResultSets[0].SearchRegion != null)
                {
     
                    if (response.ResultSets[0].SearchRegion.GeocodeLocation.Address != null
                        && response.ResultSets[0].SearchRegion.GeocodeLocation.Locations.Count > 0)
                    {
                        foreach (Location location in response.ResultSets[0].SearchRegion.GeocodeLocation.Locations)
                        {
                            MyMapItem item = new MyMapItem()
                            {
                                Title = response.ResultSets[0].SearchRegion.GeocodeLocation.Address.FormattedAddress,
                                Location = location
                            };
                            this.itemCollection.Add(item);
                            RadMap1.Center = item.Location;
                            this.informationLayer.Items.Clear();
                            this.informationLayer.Items.Add(item.Location);
                        }
                    }
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //Search(_siteModel.Latitude, _siteModel.Longitude);
            Location location= new Location(Convert.ToDouble(_siteModel.Latitude), Convert.ToDouble(_siteModel.Longitude));
            this.RadMap1.Center = location;
            MyMapItem mapItem = new MyMapItem();
            mapItem.Location= location;
            itemCollection.Add(mapItem);
        }

        // The map control allows using the Geographic Coordinate System only.
        // It supports translation the geographic coordinates to logical coordinates (from 0 to 1). Also it allows getting the pixel coordinates for current viewport of map from the geographic or logical coordinates and vice versa.
        // The SpatialReference property of RadMap has the following methods:
        // GeographicToLogical(Location) -- Converts a geographical coordinate (Longitude, Latitude) to a logical Point (0->1).
        // LogicalToGeographic(Point) -- Converts a logical Point (0->1) to a geographical coordinate (Longitude, Latitude).
 
        //The Location class contains the following functionality:
        // GetCoordinates(RadMap, Point) -- Gets geographic coordinates of the point relative to the map control screen coordinates.
        // LogicalToPixel(RadMap, Point) -- Convert a logical point to a Pixel Point on the current screen at a particular zoom level.
        // PixelToLogical(RadMap, Point) -- Convert a pixel point to a Logical Point on the current screen.
        // GetPoint(RadMap) -- Gets location of the Location structure on the given RadMap control as Point relative to the size of the control.
 


        private void Search(string lat, string lon)
        {
            SearchRequest request = new SearchRequest();
            //request.Culture = new System.Globalization.CultureInfo("zh-CN");
            request.Query = "Singapore";//string.Format("{0} {1}", lon,lat);
            
            // we can't search the lat,long directly. As 
            
            LoggerHelper.LogCCF20(string.Format("地图查询经度={1}，维度={0}",lat,lon));
            this.searchProvider.SearchAsync(request);
        }

        private void UrgentVehicleList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //UrgentData ud = (UrgentData)e.AddedItems[0];
            
        }

        private void RadContextMenu_ItemClick(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
           
        }
    }
}
